﻿using AutoMapper;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Organ.API.Application.Commands.EnterpriseCommands;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FarmSmartSleep.Organ.API.Controllers
{
    /// <summary>
    /// 企业控制器--API
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SheepController : ControllerBase
    {
        public readonly IMediator mediator;
        public EnterTree enterTree;

        public SheepController(IMediator mediator, EnterTree enterTree)
        {
            this.mediator = mediator;
            this.enterTree = enterTree;
        }

        /// <summary>
        /// 企业新增
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("EnterPriseAdd")]
        public async Task<Result> Handle(EnterpriseCreateCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 单位显示分页
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet("EnterPriseList")]
        public async Task<ResultPage<EnterDto>> Handle(int pageindex,int pagesize)
        {
            return await mediator.Send(new EnterpriseSelectCommand() { pageindex = pageindex, pagesize = pagesize });
        }

        /// <summary>
        /// 省级下拉框
        /// </summary>
        /// <returns></returns>
        [HttpPost("EnterpriseType")]
        public async Task<Result> Handle()
        {
            EnterpriseTypeCommand command = new EnterpriseTypeCommand();
            return await mediator.Send(command);
        }

        /// <summary>
        /// 市级下拉框
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("EnterpriseType1")]
        public async Task<Result> Handle1(EnterprisecityListRequestModel model)
        {
            return await mediator.Send(model);
        }

        /// <summary>
        /// 县级下拉框
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("EnterpriseType2")]
        public async Task<Result> Handle2(EnterpriseproviceListsRequestModel model)
        {
            return await mediator.Send(model);
        }

        /// <summary>
        /// 行业标签一级下拉框
        /// </summary>
        /// <returns></returns>
        [HttpPost("IndustryLabelsType")]
        public async Task<Result> Handle3()
        {
            IndustryLabelsTypeCommand command = new IndustryLabelsTypeCommand();
            return await mediator.Send(command);
        }

        /// <summary>
        /// 行业标签二级下拉框
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("IndustryType")]
        public async Task<Result> Handle4(IndustryListRequestModel model)
        {
            return await mediator.Send(model);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("Enterimage")]
        public string Upload(IFormFile file)
        {
            var fileName = file.FileName;
            fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(fileName);
            var path = Directory.GetCurrentDirectory() + @"\\wwwroot\\images\\" + fileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            };
            var url = "/images/" + fileName;
            return url;
        }

        /// <summary>
        /// 树形结构
        /// </summary>
        /// <param name="Pid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTree(int Pid = 0)
        {
            return Ok(enterTree.GetTree(Pid));
        }
    }
}
